package masterjava.common.client.gxt.base;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.util.Theme;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.ui.RootPanel;
import masterjava.common.client.gwt.ExceptionHandler;
import masterjava.common.client.gwt.IExceptionHandler;

/**
 * User: gkislin
 * Date: 7/21/11
 */
abstract public class BaseEntryPoint implements EntryPoint {
    static {
        ExceptionHandler.get().setExceptionHandler(
                new IExceptionHandler() {
                    @Override
                    public void treatException(String context, Throwable e) {
                        MessageBox.alert(context, e.getMessage(), null);
                    }
                }
        );
    }

    @Override
    public void onModuleLoad() {
        GXT.setDefaultTheme(Theme.GRAY, true);
        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
            @Override
            public void onUncaughtException(Throwable e) {
                ExceptionHandler.treatException(e);
            }
        });

        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                RootLayout rootLayout = RootLayout.get();
                load(rootLayout);
                RootPanel.get().add(rootLayout);
            }
        });
    }

    abstract protected void load(RootLayout rootLayout);
}
